﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Weather.UA.Model;

namespace Weather.UA
{
    public partial class WeatherUA
    {
        private City _city = new City();
        private readonly List<City> _cities = new List<City>();

        public City GetCityById(int id)
        {
            _city = new City();
            const string apiUrl = "{0}/{1}/city/?city={2}&lang={3}";
            var url = String.Format(apiUrl, ApiDomain, ApiVersion, id, Language);
            var document = _loadXDocument(url);
            //var serializer = new XmlSerializer(typeof(Cities));
            //var cities = (Cities)serializer.Deserialize(document.CreateReader());
            //var cccc = cities;
            var city = document.Element("city").Element("city");

            var attributeId = city.Attribute("id");
            var elementName = city.Element("name");
            var elementRegion = city.Element("region");
            var elementCountryName = city.Element("country");
            var elementCountryId = city.Element("country_id");

            if (elementName != null && attributeId != null)
            {
                _city.Id = Convert.ToInt32(attributeId.Value);
                _city.Name = elementName.Value;
                _city.Region = elementRegion.Value;
                if (elementCountryName != null && elementCountryId != null)
                {
                    _city.Country = new Country
                    {
                        Id = Convert.ToInt32(elementCountryId.Value),
                        Name = elementCountryName.Value
                    };
                }
            }
            return _city;
        }

        public List<City> GetCitiesByName(string name)
        {
            const string apiUrl = "{0}/{1}/city/?search={2}&lang={3}";
            var url = String.Format(apiUrl, ApiDomain, ApiVersion, name, Language);
            var document = _loadXDocument(url);
            //var serializer = new XmlSerializer(typeof(Cities));
            //var citiesArray = (Cities)serializer.Deserialize(document.CreateReader());
            //var cccc = citiesArray;
            var cities = document.Element("city").Descendants("city");
            foreach (var city in cities)
            {
                var attributeId = city.Attribute("id");
                var elementName = city.Element("name");
                var elementRegion = city.Element("region");
                var elementCountryName = city.Element("country");
                var elementCountryId = city.Element("country_id");

                if (elementName != null && attributeId != null)
                {
                    var c = new City();
                    c.Id = Convert.ToInt32(attributeId.Value);
                    c.Name = elementName.Value;
                    c.Region = elementRegion.Value;
                    if (elementCountryName != null && elementCountryId != null)
                    {
                        c.Country = new Country
                        {
                            Id = Convert.ToInt32(elementCountryId.Value),
                            Name = elementCountryName.Value
                        };
                    }
                    _cities.Add(c);
                }
            }
            return _cities;
        }

    }
}
